// #include<iostream>
// #include<sys/types.h>
// #include<sys/socket.h>
// #include<sys/stat.h>
// #include<fcntl.h>
// #include<unistd.h>



// class UdpServer
// {
// public:
//     bool InitServer()
//     {
//         //创建套接字
//         _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
//         if(_sockfd < 0)
//         {
//             //创建套接字失败
//             std::cerr << "socket error" << std::endl;
//             return false;
//         }

//         std::cout << "socket create success, sockfd: " << _sockfd << std :: endl;
//         return true;
//     }

//     ~UdpServer()
//     {
//         if(_sockfd >= 0)
//         {
//             close(_sockfd);
//         }
//     }

// private:
//     int _sockfd;//文件描述符
// };

// int main()
// {
//     UdpServer* ser = new UdpServer();
//     ser->InitServer();
//     sleep(10);
//     return 0;
// }



#include<iostream>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<netinet/in.h>
#include<arpa/inet.h>

class UdpServer
{
public:
    //构造函数，初始化_sockfd为-1
    UdpServer(std::string ip, int port)
      :_sockfd(-1)
      ,_port(port)
       ,_ip(ip)
    {};

    bool InitServer()
    {
        //创建套接字
        _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if(_sockfd < 0)
        {
            //创建套接字失败
            std::cerr << "socket error" << std::endl;
            return false;
        }

        std::cout << "socket create success, sockfd: " << _sockfd << std :: endl;

        //绑定套接字到一个本地地址
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(8888);//端口号
        addr.sin_addr.s_addr = inet_addr("127.0.0.1");//IP地址
        int ret = bind(_sockfd, (struct sockaddr*)&addr, sizeof(addr));
        if(ret < 0)
        {
            //绑定失败
            std::cerr << "bind error" << std::endl;
            close(_sockfd);
            return false;
        }

        std::cout << "bind success" << std::endl;
        return true;
    }

    //数据收发和处理逻辑
    void Start()
    {
        while(1)
        {
            //接收客户端发送的数据
            char buf[1024] = {0};
            struct sockaddr_in cli_addr;
            socklen_t len = sizeof(cli_addr);
            int n = recvfrom(_sockfd, buf, sizeof(buf)-1, 0, (struct sockaddr*)&cli_addr, &len);
            if(n < 0)
            {
                //接收失败
                std::cerr << "recvfrom error" << std::endl;
                continue;
            }

            std::cout << "recv from client: " << buf << std::endl;

            //处理数据，这里简单地将数据转换为大写
            for(int i = 0; i < n; i++)
            {
                buf[i] = toupper(buf[i]);
            }

            //发送数据给客户端
            n = sendto(_sockfd, buf, n, 0, (struct sockaddr*)&cli_addr, len);
            if(n < 0)
            {
                //发送失败
                std::cerr << "sendto error" << std::endl;
                continue;
            }

            std::cout << "send to client: " << buf << std::endl;
        }
    }

    ~UdpServer()
    {
        if(_sockfd >= 0)
        {
            close(_sockfd);
        }
    }

private:
    int _sockfd;//文件描述符
    int _port;//端口号
    std::string _ip; //ip地址
};

int main()
{
    UdpServer* ser = new UdpServer();
    if(ser->InitServer())
    {
        ser->Start();
    }
    
    delete ser;//释放内存
    return 0;
}
